/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package module1;

import dao.DanhMucDAO;
import dao.SanPhamDAO;
import java.util.List;
import java.util.Vector;
import javax.swing.DefaultComboBoxModel;
import javax.swing.table.DefaultTableModel;
import pojo.DanhMuc;
import pojo.SanPham;

/**
 *
 * @author OSIRIS
 */
public class TimKiemFrame extends javax.swing.JInternalFrame {

    /**
     * Creates new form TimKiemFrame
     */
    public TimKiemFrame() {
        initComponents();
    }
  public void TimKiem(){
      Vector vt1=(Vector)this.jCbxDanhMuc.getSelectedItem();
      int maDanhMuc=(Integer)vt1.get(0);
      String tenSanPham=this.jTxtTenSamPham.getText();
      List<SanPham> ds=SanPhamDAO.TimKiem(tenSanPham, maDanhMuc);
        DefaultTableModel dtm=new DefaultTableModel();
        dtm.addColumn("ID");
        
         dtm.addColumn("Ma Danh Muc");
          dtm.addColumn("Ten San Pham");
           dtm.addColumn("Gia Ban");
            dtm.addColumn("Kick Co");
             dtm.addColumn("Mau Sac");
              dtm.addColumn("So Luong");
        for (int i = 0; i < ds.size(); i++) {
           SanPham dm=ds.get(i);
            Vector vt=new Vector();
            vt.add(dm.getId());
            vt.add(dm.getDanhMuc().getId());
            vt.add(dm.getTenSanPham());
            vt.add(dm.getGiaBan());
            vt.add(dm.getKickCo());
            vt.add(dm.getMauSac());
            vt.add(dm.getSoLuong());
            dtm.addRow(vt);
        }
        this.jTable1.setModel(dtm);
  }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jTxtTenSamPham = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jCbxDanhMuc = new javax.swing.JComboBox();
        btnTimKiem = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();

        setClosable(true);
        setTitle("Tim Kiếm Nâng Cao");
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameOpened(evt);
            }
        });

        jLabel1.setText("Tên Sản Phẩm");

        jTxtTenSamPham.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                jTxtTenSamPhamCaretUpdate(evt);
            }
        });

        jLabel2.setText("Danh Mục");

        jCbxDanhMuc.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCbxDanhMucActionPerformed(evt);
            }
        });

        btnTimKiem.setText("Tìm Kiếm");
        btnTimKiem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTimKiemActionPerformed(evt);
            }
        });

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(jTable1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 655, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(47, 47, 47)
                        .addComponent(jTxtTenSamPham, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(56, 56, 56)
                        .addComponent(jLabel2)
                        .addGap(46, 46, 46)
                        .addComponent(jCbxDanhMuc, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(50, 50, 50)
                        .addComponent(btnTimKiem)))
                .addContainerGap(18, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTxtTenSamPham, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(jCbxDanhMuc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnTimKiem))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 282, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(15, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formInternalFrameOpened(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameOpened
        // TODO add your handling code here:
        //List<DanhMuc> ds=DanhMucDAO.getList();
         DanhMucDAO dmDao = new DanhMucDAO();
        List ds = dmDao.findAll();
        DefaultComboBoxModel dcm=new DefaultComboBoxModel();
        
        for (int i = 0; i < ds.size(); i++) {
           DanhMuc dm=(DanhMuc)ds.get(i);
            Vector vt=new Vector();
            vt.add(dm.getId());
            vt.add(dm.getTen());
            dcm.addElement(vt);
        }
        this.jCbxDanhMuc.setModel(dcm);
    }//GEN-LAST:event_formInternalFrameOpened

    private void btnTimKiemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTimKiemActionPerformed
        // TODO add your handling code here:
        this.TimKiem();
    }//GEN-LAST:event_btnTimKiemActionPerformed

    private void jTxtTenSamPhamCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_jTxtTenSamPhamCaretUpdate
        // TODO add your handling code here:
        this.TimKiem();
        
    }//GEN-LAST:event_jTxtTenSamPhamCaretUpdate

    private void jCbxDanhMucActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCbxDanhMucActionPerformed
        // TODO add your handling code here:
        this.TimKiem();
    }//GEN-LAST:event_jCbxDanhMucActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnTimKiem;
    private javax.swing.JComboBox jCbxDanhMuc;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTxtTenSamPham;
    // End of variables declaration//GEN-END:variables
}
